package com.wx.resume_excel.utils;

import com.wx.resume_excel.model.JobSeeker;
import com.wx.resume_excel.model.ResumeTemplate;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 本模块中有关excel的工具类
 *
 * @Auther 田艺鹏
 * @DateTime 2020-08-03 10:51
 */
public class ExcelUtil {

    /**
     * 创建模板表
     *
     * @param templateFillData 实体类数据
     * @return 有具体数据的list集合
     */
    public static List createTable ( ResumeTemplate templateFillData ) {
        // 第一个表格所有行的集合
        List<List<Object>> list = new ArrayList<List<Object>> ( );

        List<Object> titleRow1 = new ArrayList<Object> ( );
        List<Object> titleRow2 = new ArrayList<Object> ( );
        titleRow1.add ( "个人简历" );

        list.add ( titleRow1 );

        List<Object> content1Row1 = new ArrayList<Object> ( );
        content1Row1.add ( "应聘职位" );
        content1Row1.add ( templateFillData.getPositionApplicied ( ) );

        List<Object> content1Row2 = new ArrayList<Object> ( );
        content1Row2.add ( "应聘公司" );
        content1Row2.add ( templateFillData.getCompany ( ) );

        List<Object> content1Row3 = new ArrayList<Object> ( );
        content1Row3.add ( "投递时间" );
        content1Row3.add ( templateFillData.getDeliveryTime ( ) );

        List<Object> content1Row4 = new ArrayList<Object> ( );
        content1Row4.add ( "应聘时间" );
        content1Row4.add ( templateFillData.getUpdateTime ( ) );

        List<Object> content1Row5 = new ArrayList<Object> ( );
        content1Row5.add ( "" );
        content1Row5.add ( "" );

        List<Object> content1Row6 = new ArrayList<Object> ( );
        content1Row6.add ( "匹配度" );
        content1Row6.add ( templateFillData.getMatchingDegree ( ) );

        list.add ( content1Row1 );
        list.add ( content1Row2 );
        list.add ( content1Row3 );
        list.add ( content1Row4 );
        list.add ( content1Row5 );
        list.add ( content1Row6 );

        List<Object> content2Row1 = new ArrayList<Object> ( );
        content2Row1.add ( "个人信息" );
        content2Row1.add ( "" );

        List<Object> content2Row2 = new ArrayList<Object> ( );
        content2Row2.add ( "姓名" );
        content2Row2.add ( templateFillData.getName ( ) );

        List<Object> content2Row3 = new ArrayList<Object> ( );
        content2Row3.add ( "性别" );
        content2Row3.add ( templateFillData.getGender ( ) );

        List<Object> content2Row4 = new ArrayList<Object> ( );
        content2Row4.add ( "出生日期" );
        content2Row4.add ( templateFillData.getBirthday ( ) );

        List<Object> content2Row5 = new ArrayList<Object> ( );
        content2Row5.add ( "工作年限" );
        content2Row5.add ( templateFillData.getWorkingYears ( ) );

        List<Object> content2Row6 = new ArrayList<Object> ( );
        content2Row6.add ( "手机" );
        content2Row6.add ( templateFillData.getCallPhone ( ) );

        List<Object> content2Row7 = new ArrayList<Object> ( );
        content2Row7.add ( "求职状态" );
        content2Row7.add ( templateFillData.getApplicationStatus ( ) );

        List<Object> content2Row8 = new ArrayList<Object> ( );
        content2Row8.add ( "邮箱" );
        content2Row8.add ( templateFillData.getEmail ( ) );

        List<Object> content2Row9 = new ArrayList<Object> ( );
        content2Row9.add ( "居住地" );
        content2Row9.add ( templateFillData.getLiveAddress ( ) );

        List<Object> content2Row10 = new ArrayList<Object> ( );
        content2Row10.add ( "户口/国籍" );
        content2Row10.add ( templateFillData.getNationality ( ) );

        List<Object> content2Row11 = new ArrayList<Object> ( );
        content2Row11.add ( "婚姻状况" );
        content2Row11.add ( templateFillData.getMaritalStatus ( ) );

        List<Object> content2Row12 = new ArrayList<Object> ( );
        content2Row12.add ( "政治面貌" );
        content2Row12.add ( templateFillData.getPoliticalOutlook ( ) );

        List<Object> content2Row13 = new ArrayList<Object> ( );
        content2Row13.add ( "家庭电话" );
        content2Row13.add ( templateFillData.getHomePhone ( ) );

        List<Object> content2Row14 = new ArrayList<Object> ( );
        content2Row14.add ( "身高" );
        content2Row14.add ( templateFillData.getHeight ( ) );

        List<Object> content2Row15 = new ArrayList<Object> ( );
        content2Row15.add ( "家庭地址" );
        content2Row15.add ( templateFillData.getAddress ( ) );

        List<Object> content2Row16 = new ArrayList<Object> ( );
        content2Row16.add ( "邮编" );
        content2Row16.add ( templateFillData.getPostCode ( ) );

        List<Object> content2Row17 = new ArrayList<Object> ( );
        content2Row17.add ( "个人主页" );
        content2Row17.add ( templateFillData.getPersonPage ( ) );

        list.add ( content2Row1 );
        list.add ( content2Row2 );
        list.add ( content2Row3 );
        list.add ( content2Row4 );
        list.add ( content2Row5 );
        list.add ( content2Row6 );
        list.add ( content2Row7 );
        list.add ( content2Row8 );
        list.add ( content2Row9 );
        list.add ( content2Row10 );
        list.add ( content2Row11 );
        list.add ( content2Row12 );
        list.add ( content2Row13 );
        list.add ( content2Row14 );
        list.add ( content2Row15 );
        list.add ( content2Row16 );
        list.add ( content2Row17 );

        List<Object> content3Row1 = new ArrayList<Object> ( );
        content3Row1.add ( "目前年收入" );
        content3Row1.add ( "" );

        List<Object> content3Row2 = new ArrayList<Object> ( );
        content3Row2.add ( "目前年收入" );
        content3Row2.add ( templateFillData.getAnnualIncome ( ) );

        List<Object> content3Row3 = new ArrayList<Object> ( );
        content3Row3.add ( "基本工资(税前)" );
        content3Row3.add ( templateFillData.getBasicIncome ( ) );

        List<Object> content3Row4 = new ArrayList<Object> ( );
        content3Row4.add ( "补贴/津贴" );
        content3Row4.add ( templateFillData.getOtherIncome ( ) );

        List<Object> content3Row5 = new ArrayList<Object> ( );
        content3Row5.add ( "奖金/佣金" );
        content3Row5.add ( templateFillData.getBonus ( ) );

        List<Object> content3Row6 = new ArrayList<Object> ( );
        content3Row6.add ( "股权收益" );
        content3Row6.add ( templateFillData.getEquityIncome ( ) );

        list.add ( content3Row1 );
        list.add ( content3Row2 );
        list.add ( content3Row3 );
        list.add ( content3Row4 );
        list.add ( content3Row5 );
        list.add ( content3Row6 );

        List<Object> content4Row1 = new ArrayList<Object> ( );
        content4Row1.add ( "求职意向" );
        content4Row1.add ( "" );

        List<Object> content4Row2 = new ArrayList<Object> ( );
        content4Row2.add ( "关键字" );
        content4Row2.add ( templateFillData.getKeyWord ( ) );

        List<Object> content4Row3 = new ArrayList<Object> ( );
        content4Row3.add ( "期望薪资" );
        content4Row3.add ( templateFillData.getWishIncome ( ) );

        List<Object> content4Row4 = new ArrayList<Object> ( );
        content4Row4.add ( "地点" );
        content4Row4.add ( templateFillData.getLocation ( ) );

        List<Object> content4Row5 = new ArrayList<Object> ( );
        content4Row5.add ( "职能/职位" );
        content4Row5.add ( templateFillData.getPosition ( ) );

        List<Object> content4Row6 = new ArrayList<Object> ( );
        content4Row6.add ( "行业" );
        content4Row6.add ( templateFillData.getIndustry ( ) );

        List<Object> content4Row7 = new ArrayList<Object> ( );
        content4Row7.add ( "到岗时间" );
        content4Row7.add ( templateFillData.getToTime ( ) );

        List<Object> content4Row8 = new ArrayList<Object> ( );
        content4Row8.add ( "工作类型" );
        content4Row8.add ( templateFillData.getWorkType ( ) );

        List<Object> content4Row9 = new ArrayList<Object> ( );
        content4Row9.add ( "自我评价" );
        content4Row9.add ( templateFillData.getEvaluate ( ) );

        list.add ( content4Row1 );
        list.add ( content4Row2 );
        list.add ( content4Row3 );
        list.add ( content4Row4 );
        list.add ( content4Row5 );
        list.add ( content4Row6 );
        list.add ( content4Row7 );
        list.add ( content4Row8 );
        list.add ( content4Row9 );

        List<Object> content5Row1 = new ArrayList<Object> ( );
        content5Row1.add ( "工作经验" );
        content5Row1.add ( "" );

        List<Object> content5Row2 = new ArrayList<Object> ( );
        content5Row2.add ( "工作经验" );
        content5Row2.add ( templateFillData.getWorkExperience1 ( ) );

        List<Object> content5Row3 = new ArrayList<Object> ( );
        content5Row3.add ( "工作经验" );
        content5Row3.add ( templateFillData.getWorkExperience2 ( ) );

        List<Object> content5Row4 = new ArrayList<Object> ( );
        content5Row4.add ( "工作经验" );
        content5Row4.add ( templateFillData.getWorkExperience3 ( ) );

        List<Object> content5Row5 = new ArrayList<Object> ( );
        content5Row5.add ( "工作经验" );
        content5Row5.add ( templateFillData.getWorkExperience4 ( ) );

        List<Object> content5Row6 = new ArrayList<Object> ( );
        content5Row6.add ( "工作经验" );
        content5Row6.add ( templateFillData.getWorkExperience5 ( ) );

        List<Object> content5Row7 = new ArrayList<Object> ( );
        content5Row7.add ( "工作经验" );
        content5Row7.add ( templateFillData.getWorkExperience6 ( ) );

        List<Object> content5Row8 = new ArrayList<Object> ( );
        content5Row8.add ( "工作经验" );
        content5Row8.add ( templateFillData.getWorkExperience7 ( ) );

        List<Object> content5Row9 = new ArrayList<Object> ( );
        content5Row9.add ( "工作经验" );
        content5Row9.add ( templateFillData.getWorkExperience8 ( ) );

        List<Object> content5Row10 = new ArrayList<Object> ( );
        content5Row10.add ( "工作经验" );
        content5Row10.add ( templateFillData.getWorkExperience9 ( ) );

        List<Object> content5Row11 = new ArrayList<Object> ( );
        content5Row11.add ( "工作经验" );
        content5Row11.add ( templateFillData.getWorkExperience10 ( ) );

        list.add ( content5Row1 );
        list.add ( content5Row2 );
        list.add ( content5Row3 );
        list.add ( content5Row4 );
        list.add ( content5Row5 );
        list.add ( content5Row6 );
        list.add ( content5Row7 );
        list.add ( content5Row8 );
        list.add ( content5Row9 );
        list.add ( content5Row10 );
        list.add ( content5Row11 );

        List<Object> content6Row1 = new ArrayList<Object> ( );
        content6Row1.add ( "项目经验" );
        content6Row1.add ( "" );

        List<Object> content6Row2 = new ArrayList<Object> ( );
        content6Row2.add ( "项目经验" );
        content6Row2.add ( templateFillData.getProjectExperience1 ( ) );

        List<Object> content6Row3 = new ArrayList<Object> ( );
        content6Row3.add ( "项目经验" );
        content6Row3.add ( templateFillData.getProjectExperience2 ( ) );

        List<Object> content6Row4 = new ArrayList<Object> ( );
        content6Row4.add ( "项目经验" );
        content6Row4.add ( templateFillData.getProjectExperience3 ( ) );

        List<Object> content6Row5 = new ArrayList<Object> ( );
        content6Row5.add ( "项目经验" );
        content6Row5.add ( templateFillData.getProjectExperience4 ( ) );

        List<Object> content6Row6 = new ArrayList<Object> ( );
        content6Row6.add ( "项目经验" );
        content6Row6.add ( templateFillData.getProjectExperience5 ( ) );

        List<Object> content6Row7 = new ArrayList<Object> ( );
        content6Row7.add ( "项目经验" );
        content6Row7.add ( templateFillData.getProjectExperience6 ( ) );

        List<Object> content6Row8 = new ArrayList<Object> ( );
        content6Row8.add ( "项目经验" );
        content6Row8.add ( templateFillData.getProjectExperience7 ( ) );

        List<Object> content6Row9 = new ArrayList<Object> ( );
        content6Row9.add ( "项目经验" );
        content6Row9.add ( templateFillData.getProjectExperience8 ( ) );

        List<Object> content6Row10 = new ArrayList<Object> ( );
        content6Row10.add ( "项目经验" );
        content6Row10.add ( templateFillData.getProjectExperience9 ( ) );

        List<Object> content6Row11 = new ArrayList<Object> ( );
        content6Row11.add ( "项目经验" );
        content6Row11.add ( templateFillData.getProjectExperience10 ( ) );

        list.add ( content6Row1 );
        list.add ( content6Row2 );
        list.add ( content6Row3 );
        list.add ( content6Row4 );
        list.add ( content6Row5 );
        list.add ( content6Row6 );
        list.add ( content6Row7 );
        list.add ( content6Row8 );
        list.add ( content6Row9 );
        list.add ( content6Row10 );
        list.add ( content6Row11 );

        List<Object> content7Row1 = new ArrayList<Object> ( );
        content7Row1.add ( "教育经历" );
        content7Row1.add ( "" );

        List<Object> content7Row2 = new ArrayList<Object> ( );
        content7Row2.add ( "教育经历" );
        content7Row2.add ( templateFillData.getEducationExperience ( ) );

        list.add ( content7Row1 );
        list.add ( content7Row2 );

        List<Object> content8Row1 = new ArrayList<Object> ( );
        content8Row1.add ( "在校情况" );
        content8Row1.add ( "" );

        List<Object> content8Row2 = new ArrayList<Object> ( );
        content8Row2.add ( "校内荣誉" );
        content8Row2.add ( templateFillData.getCampusHonor ( ) );

        List<Object> content8Row3 = new ArrayList<Object> ( );
        content8Row3.add ( "校内职务" );
        content8Row3.add ( templateFillData.getCampusPosition ( ) );

        list.add ( content8Row1 );
        list.add ( content8Row2 );
        list.add ( content8Row3 );

        List<Object> content9Row1 = new ArrayList<Object> ( );
        content9Row1.add ( "技能特长" );
        content9Row1.add ( "" );

        List<Object> content9Row2 = new ArrayList<Object> ( );
        content9Row2.add ( "技能/语言" );
        content9Row2.add ( templateFillData.getSkills ( ) );

        List<Object> content9Row3 = new ArrayList<Object> ( );
        content9Row3.add ( "证书" );
        content9Row3.add ( templateFillData.getCertificate ( ) );

        List<Object> content9Row4 = new ArrayList<Object> ( );
        content9Row4.add ( "培训经历" );
        content9Row4.add ( templateFillData.getTrainingExperience ( ) );

        list.add ( content9Row1 );
        list.add ( content9Row2 );
        list.add ( content9Row3 );
        list.add ( content9Row4 );

        List<Object> content10Row1 = new ArrayList<Object> ( );
        content10Row1.add ( "附加信息" );
        content10Row1.add ( "" );

        List<Object> content10Row2 = new ArrayList<Object> ( );
        content10Row2.add ( "其他" );
        content10Row2.add ( templateFillData.getOtherMsg ( ) );

        list.add ( content10Row1 );
        list.add ( content10Row2 );

        List<Object> content11Row1 = new ArrayList<Object> ( );
        content11Row1.add ( "求职信" );
        content11Row1.add ( "" );

        List<Object> content11Row2 = new ArrayList<Object> ( );
        content11Row2.add ( "求职信" );
        content11Row2.add ( templateFillData.getCoverLetter ( ) );

        list.add ( content11Row1 );
        list.add ( content11Row2 );

        return list;
    }

    /**
     * 判断目标文件是否存在以及对应文件格式,返回一个具体的文件地址
     *
     * @param fileName 文件名
     * @return 文件路径
     */
    public static String fileIsExits ( String fileName ) {
        String filePath = "";

        File file = new File ( "F:/tns-server/tns-resume-excel/src/main/resources/static/" + fileName + ".xlsx" );
        if (!file.exists ( )) {
            file = new File ( "F:/tns-server/tns-resume-excel/src/main/resources/static/" + fileName + ".xls" );
            if (!file.exists ( )) {
                filePath = "";
            } else {
                filePath = file.getPath ( );
            }
        } else {
            filePath = file.getPath ( );
        }

        return filePath;
    }

    /**
     * 读取文档首页的数据 并返回一个JobSeeker对象
     *
     * @param o 文档每一行的数据对象
     * @return 返回一个封装好数据信息的JobSeeker对象
     */
    public static JobSeeker readJobSeeker ( Object o ) {
        String jobSeekerStr = o.toString ( );
        jobSeekerStr = jobSeekerStr.replace ( "[","" );
        jobSeekerStr = jobSeekerStr.replace ( "]","" );
        String[] attributes = jobSeekerStr.split ( "," );
        String[] finalAttribute = new String[attributes.length];

        for (int i = 0; i <= attributes.length - 1; i++) {
            if (i == attributes.length - 1) {
                attributes[i] = attributes[i].replaceAll ( "}","" );
            }
            String[] strs = attributes[i].split ( "=" );
            finalAttribute[i] = strs[1];
        }


        JobSeeker jobSeeker = new JobSeeker ( finalAttribute[0],finalAttribute[1],finalAttribute[2],finalAttribute[3],
                finalAttribute[4],finalAttribute[5],finalAttribute[6],finalAttribute[7],
                finalAttribute[8],finalAttribute[9],finalAttribute[10],finalAttribute[11],
                finalAttribute[12],finalAttribute[13],finalAttribute[14],finalAttribute[15],
                finalAttribute[16],finalAttribute[17],finalAttribute[18],finalAttribute[19],
                finalAttribute[20],finalAttribute[21],finalAttribute[22],finalAttribute[23],
                finalAttribute[24] );
        return jobSeeker;
    }

    /**
     * 创建文档写入数据时创建文档名字的方法
     *
     * @param fileName 默认文件名
     * @return 可以使用的文件名
     */
    public static String createFileName ( String fileName ) {
        File file = new File ( "F:/tns-server/tns-resume-excel/src/main/resources/static/" + fileName + ".xlsx" );
        if (file.exists ( )) {
            String[] suffix = fileName.split ( "_" );
            int suffixNum = 0;
            if (suffix.length >= 2) {
                suffixNum = Integer.parseInt ( suffix[1] );
                ++suffixNum;
                return createFileName ( suffix[0] + "_" + suffixNum );
            }
            return createFileName ( suffix[0] + "_1" );
        } else {
            return fileName;
        }
    }

    /**
     * 根据时间判断文件名的后缀个数
     *
     * @param time 时间
     * @return 后缀个数
     */
    public static int getFileName ( String time ) {
        int date = 0;
        String[] realTime = time.split ( ":" );
        switch (realTime[1]) {
            case "01":
            case "03":
            case "05":
            case "07":
            case "08":
            case "10":
            case "12":
                date = 31;
                break;

            case "04":
            case "06":
            case "09":
            case "11":
                date = 30;
                break;

            case "02":
                int year = Integer.parseInt ( realTime[0] );
                if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0 && year % 100 == 0)) {
                    System.out.println ( "是闰年" );
                    date = 29;
                } else {
                    date = 28;
                    System.out.println ( "不是闰年" );
                }
                break;
        }

        return date;
    }

    /**
     * 获取指定文件夹中文件的个数
     *
     * @return
     */
    public static int getFileCount () {
        File file = new File ( "F:/tns-server/tns-resume-excel/src/main/resources/static/" );
        File[] files = file.listFiles ( );
        return files.length;
    }

    /**
     * 创建8(日期） + 8（电话号码后8位） 位数字的简历编号
     * 首次添加简历编号时 使用
     *
     * @return
     */
    public static String createResumeNo ( String callPhone ) {
        SimpleDateFormat sdf = new SimpleDateFormat ( "yyyyMMdd" );
        String time1 = sdf.format ( new Date ( ) );
        String time2 = callPhone.substring ( 3,callPhone.length ( ) );
        return time1 + time2;
    }

    /**
     * 创建随机8(日期） + 8(随机数) 位数字的简历编号
     * 数据库存在编号后重新获取随机编号时使用
     *
     * @return
     */
    public static String createRoundResumeNo () {
        SimpleDateFormat sdf = new SimpleDateFormat ( "yyyyMMdd" );
        String time1 = sdf.format ( new Date ( ) );
        String sum = Math.random ( ) + "";
        String time2 = sum.substring ( 2,10 );
        return time1 + time2;
    }

}
